/*
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.model;

import java.lang.reflect.InvocationTargetException;

import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.ui.IPerforceUIConstants;
import net.sourceforge.perforce.ui.PerforceUIPlugin;
import net.sourceforge.perforce.ui.internal.Policy;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
  * @version $Revision: 42 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class DepotContainerElement extends PerforceModelElement 
                                   implements IWorkbenchAdapter, IAdaptable
{	
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the Perforce Server. */
	private IPerforceServer server;
	
	// ------------------------------------------------------------------------- Public Constructors
	
	/** Constructor for DepotContainerElement. 
	  * 
	  * @param server reference to the Perforce server.
	  */
	public DepotContainerElement(IPerforceServer server)
	{
		this.server = server;
	}
	
	// ---------------------------------------------------------- Public Methods (IWorkbenchAdapter)
	
	/** @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(Object) */
	public Object[] getChildren(Object o) 
	{
		if (!(o instanceof DepotContainerElement))
		{
			return new Object[0];
		}
		
		final Object[][] result = new Object[1][];

		Shell shell = new Shell(Display.getDefault());
		try 
		{
			run(true, false, new IRunnableWithProgress() 
			{
				/** @see IRunnableWithProgress#run(IProgressMonitor) */
				public void run(IProgressMonitor monitor)
					throws InvocationTargetException, InterruptedException 
				{
					try
					{
						result[0] = getServer().members(monitor);
					}
					catch (PerforceException e)
					{
						throw new InvocationTargetException(e);
					}
				}
			});		
		} 
		catch (InvocationTargetException e) 
		{
			handle(e);
		} 
		catch (InterruptedException e) 
		{
			handle(e);
		}
		finally
		{
			shell.dispose();
		}
		
		return result[0];
	}
	
	/** @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(Object) */
	public ImageDescriptor getImageDescriptor(Object o) 
	{
		if (!(o instanceof DepotContainerElement))
		{
			return null;
		}
		
		return PerforceUIPlugin.getPlugin().getImageDescriptor(IPerforceUIConstants.IMG_HEAD_REV);
	}

	/** @see org.eclipse.ui.model.IWorkbenchAdapter#getLabel(Object) */
	public String getLabel(Object o) 
	{
		if (!(o instanceof DepotContainerElement))
		{
			return null;
		}

		return Policy.bind("DepotContainerElement.getLabel");
	}

	/** @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(Object) */
	public Object getParent(Object o) 
	{
		return null;
	}

	// ----------------------------------------------------------------- Public Methods (IAdaptable)
	
	/** @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class) */
	public Object getAdapter(Class adapter) 
	{
		if (adapter == IWorkbenchAdapter.class)
		{
			return this;
		}
		
		return null;
	}

	// ------------------------------------------------------------------------------ Public Methods
	
	/** Call this method to get the server reference.
	  * 
	  * @return the perforce server.
	  */
	public IPerforceServer getServer()
	{
		return server;
	}
}
